/**
 * Created by Administrator on 2018-3-15.
 */
/**
 * Created by Administrator on 2018-3-8.
 */
//处理界面传递过来的续办相关数据
var summaryBO;
var summary;
//顶部开关按钮选择是否要续办
var isContinue=true;
//选择的续办人员
var selectMember;
var pageX = {
    exeLock : false
}

//判断字符是否为空的方法
function isEmpty(obj){
    if(typeof obj == "undefined" || obj == null || obj == ""){
        return true;
    }else{
        return false;
    }
}

cmp.ready(function(){
    var param = cmp.href.getParam();
    summaryBO = param.summaryBO;//获取由处理界面传递过来的参数
    summary=summaryBO["summary"];
    cmp.scrollBox("#scroll");//启动全屏滑动功能
    _initView();
    _initEvent();
    _isContinue();
    _selectContinueMembers();
});

function _initEvent(){
    _initBackEvent();
    _$("#submit").addEventListener("tap", _submit);
    _more();
}
function _initBackEvent(){
    cmp.backbutton();
    cmp.backbutton.push(_goBack);
    _$("#goBackBtn").addEventListener("tap", _goBack);
}

//返回上一个页面
function _goBack(){
    //防止爆点
    if(pageX.exeLock){
        return;
    }
    pageX.exeLock = true;
    cmp.href.back();
}

//初始化界面
function _initView(){
    var permissions=summary.permissions;
    var membersObj =eval('('+summary.memberJson+')');
    for(var i = 0,len = permissions.length;i < len; i++){
        var permName=permissions[i].name;
        var continueMembers=membersObj[permName];
        permissions[i].continueMembers=continueMembers;
    }
    var pendingTPL = _$("#list_li_tpl").innerHTML;
    var html = cmp.tpl(pendingTPL, permissions);
    cmp.append(_$("#ul_conninue"),html);
}
//顶部续办开关按钮
function _isContinue(){
    cmp.init({
        swipeBack:true //启用右滑关闭功能
    });
    cmp('.cmp-content .cmp-switch').each(function() {
        this.addEventListener('toggle', function(event) {
            if(event.detail.isActive==true){
                _$("#ul_conninue").style.display = "block";
                isContinue=true;
            }else{
                _$("#ul_conninue").style.display = "none";
                isContinue=false;
            }
        });
    });
}

//点击选择更多
function _more(){
    cmp('.cmp-table-div-more').each(function() {
        this.addEventListener('tap', function(event) {
            if(!isEmpty(selectMember)){
                cmp.notification.toast(cmp.i18n("collaboration.details.deal.continueonly"),"center");
                return;
            }
            var permissionSelected=this.getAttribute("data-permission");
            //选人界面调用
            cmp.selectOrg("select_org_1_" + (new Date()).getTime(), {
                "type":1,
                "flowType" : 1,
                "label" : ["dept", "org", "post", "team", "extP", "vjOrg"],
                "directDepartment" : false,
                "flowOptsChange" : true,
                lightOptsChange : false,//轻表单模式选人
                fillBackData:[],//初始化数据
                jump:false,
                excludeData:null,
                maxSize:1,
                minSize:1,
                accountID:"",
                permission : true,
                callback: function(datas){
                    var result = cmp.parseJSON(datas);
                    var orgMembers = result.orgResult;
                    selectMember=new Object();
                    selectMember.id = orgMembers[0].id;//为对象增加属性
                    selectMember.name = orgMembers[0].name;
                    selectMember.entityType =orgMembers[0].type;
                    selectMember.excludeChildDepartment = "";
                    selectMember.accountId = orgMembers[0].account;
                    selectMember.accountName=orgMembers[0].accountName;
                    selectMember.policyId=summary.permissions[permissionSelected].name;
                    selectMember.policyName=summary.permissions[permissionSelected].label;
                    selectMember.policyIndex=permissionSelected;
                    _submit();
                },
                closeCallback: function(){
                    console.log("close, select nobody ");
                }
            });
        });
    });
}

//选择续办人员
function _selectContinueMembers(){
    cmp('.select_continue_members').each(function() {
        this.addEventListener('tap', function() {
            var membersAvatar=this.getElementsByClassName("select_continue_members-avatar");
            var imgAvatarSelected=membersAvatar[0].getElementsByClassName("cmp-icon-avatar-selected");
            if(imgAvatarSelected[0].style.display=="none" || imgAvatarSelected[0].style.display==""){
                if(!isEmpty(selectMember)){
                    cmp.notification.toast(cmp.i18n("collaboration.details.deal.continueonly"),"center");
                    return;
                }
                imgAvatarSelected[0].style.display="block";
                var memberid=this.getAttribute("data-memberid");
                var membername=this.getAttribute("data-membername");
                var memberaccountid=this.getAttribute("data-memberaccountid");
                var permissionsname=this.getAttribute("data-permissionsname");
                var permissionslabel=this.getAttribute("data-permissionslabel");
                var permissionsindex=this.getAttribute("data-permissionsindex");
                selectMember=new Object();
                selectMember.id = memberid;//为对象增加属性
                selectMember.name = membername;
                selectMember.entityType ="Member";
                selectMember.excludeChildDepartment = "";
                selectMember.accountId = memberaccountid;
                selectMember.accountName="";
                selectMember.policyId=permissionsname;
                selectMember.policyName=permissionslabel;
                selectMember.policyIndex=permissionsindex;
            }else{
                imgAvatarSelected[0].style.display="none";
                selectMember=null;
            }
        });
    });
}
//确定提交
function _submit(){
    if(isContinue){
        if(selectMember==null){
            cmp.notification.toast(cmp.i18n("collaboration.details.deal.continueselect"),"center");
            return ;
        }
        cmp.href.back(1,{
            data:{isBackFromContinnue:true,isContinnue:true,backData:selectMember}
        });
    }else{
        cmp.href.back(1,{
            data:{isBackFromContinnue:true,isContinnue:false}
        });
    }
}